package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.cndragon.R;
import com.sskj.cndragon.presenter.RegisterActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.rl_see)
    CheckBox rlSee;
    @BindView(R.id.rl_see_confirm)
    CheckBox rlSeeConfirm;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_mobile_code)
    EditText etMobileCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pwd_again)
    EditText etPwdAgain;
    @BindView(R.id.et_invite_code)
    EditText etInviteCode;
    @BindView(R.id.cb_read)
    CheckBox cbRead;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.btn_register)
    Button btnRegister;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterActivityPresenter getPresenter() {
        return new RegisterActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        rlSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        rlSeeConfirm.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else
                etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        });
        ClickUtil.click(300, tvGetCode, () -> {
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunindeshoujihao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etMobile))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            mPresenter.getCode(getEtext(etMobile), BaseHttpConfig.CODE_REGISTER);
        });
        ClickUtil.click(300, btnRegister, () -> {
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunindeshoujihao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etMobile))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            if (isEmpty(etMobileCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruduanxinyanzhengma));
                return;
            }
            if (isEmpty(etPwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurudenglumima));
                return;
            }
            if (!CheckUtil.isPayPwd(getEtext(etPwd))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedemima));
                return;
            }
            if (!getEtext(etPwd).equals(getEtext(etPwdAgain))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingmimabuyizhi));
                return;
            }
            if (isEmpty(etInviteCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruyaoqingma));
                return;
            }
            if (!cbRead.isChecked()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingyuedubingtongyi));
                return;
            }
            mPresenter.register(getEtext(etMobile),
                    getEtext(etMobileCode),
                    getEtext(etPwd),
                    getEtext(etInviteCode));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public void getCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void registerSuccess() {
        finish();
    }
}
